package com.lly.songhigh.fragment;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.google.gson.Gson;
import com.lly.songhigh.R;
import com.lly.songhigh.adapter.SongSheetListAdapter;
import com.lly.songhigh.constant.StringResouce;
import com.lly.songhigh.constant.URL;
import com.lly.songhigh.entity.SongSheetList;
import com.lly.songhigh.view.NoScrollGridView;
import com.lly.songhigh.volley.StringRequestUtf;
import com.lly.songhigh.volley.VolleySingleton;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;

/**
 * Created by dllo on 15/10/22.
 */
public class SongSheetFragment extends BaseFragment implements RadioGroup.OnCheckedChangeListener, View.OnClickListener, AdapterView.OnItemClickListener {
    private RadioButton songSheetRadioButtonRecently;
    private RadioGroup songSheetRadioGroup;
    private TextView songSheetTotalTv;
    private RadioButton songSheetRadioButtonHot;
    private TextView songSheetListTagZero;
    private NoScrollGridView songSheetListGridZero;
    private TextView songSheetListTagOne;
    private NoScrollGridView songSheetListGridOne;
    private TextView songSheetListTagTwo;
    private NoScrollGridView songSheetListGridTwo;
    private TextView songSheetListTagThree;
    private NoScrollGridView songSheetListGridThree;
    private TextView songSheetListTagFour;
    private NoScrollGridView songSheetListGridFour;
    private RelativeLayout songSheetRelative;
    private LinearLayout songSheetListLinear;
    private TextView songSheetListTotal;
    private FragmentManager manager;
    private FragmentTransaction transaction;
    private VolleySingleton volleySingleton;
    private List<SongSheetList.DataEntity> dataEntities;
    private SongSheetListAdapter adapter;
    private String classify;
    private String list;
    private SongSheetClassifyFragment songSheetClassifyFragment;
    private Drawable right_drawable;
    private Drawable bottom_drawable;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_songsheet, null);
        initView(view);
        return view;
    }

    public void initView(View view) {
        songSheetRadioButtonRecently = (RadioButton) view.findViewById(R.id.song_sheet_radio_button_recently);
        songSheetRadioGroup = (RadioGroup) view.findViewById(R.id.song_sheet_radio_group);
        songSheetTotalTv = (TextView) view.findViewById(R.id.song_sheet_total_tv);
        songSheetRadioButtonHot = (RadioButton) view.findViewById(R.id.song_sheet_radio_button_hot);
        songSheetListTagZero = (TextView) view.findViewById(R.id.song_sheet_list_tag_zero);
        songSheetListGridZero = (NoScrollGridView) view.findViewById(R.id.song_sheet_list_grid_zero);
        songSheetListTagOne = (TextView) view.findViewById(R.id.song_sheet_list_tag_one);
        songSheetListGridOne = (NoScrollGridView) view.findViewById(R.id.song_sheet_list_grid_one);
        songSheetListTagTwo = (TextView) view.findViewById(R.id.song_sheet_list_tag_two);
        songSheetListGridTwo = (NoScrollGridView) view.findViewById(R.id.song_sheet_list_grid_two);
        songSheetListTagThree = (TextView) view.findViewById(R.id.song_sheet_list_tag_three);
        songSheetListGridThree = (NoScrollGridView) view.findViewById(R.id.song_sheet_list_grid_three);
        songSheetListTagFour = (TextView) view.findViewById(R.id.song_sheet_list_tag_four);
        songSheetListGridFour = (NoScrollGridView) view.findViewById(R.id.song_sheet_list_grid_four);
        songSheetRelative = (RelativeLayout) view.findViewById(R.id.song_sheet_relative);
        songSheetListLinear = (LinearLayout) view.findViewById(R.id.song_sheet_list_linear);
        songSheetListTotal = (TextView) view.findViewById(R.id.song_sheet_list_total);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        songSheetClassifyFragment = new SongSheetClassifyFragment();
        songSheetRadioGroup.setOnCheckedChangeListener(this);
        songSheetTotalTv.setOnClickListener(this);
        volleySingleton = VolleySingleton.getInstance();
        bottom_drawable = getResources().getDrawable(R.mipmap.img_arrow_bottom);
        bottom_drawable.setBounds(0, 0, bottom_drawable.getMinimumWidth(), bottom_drawable.getMinimumHeight());
        right_drawable = getResources().getDrawable(R.mipmap.img_arrow_right);
        right_drawable.setBounds(0, 0, right_drawable.getMinimumWidth(), right_drawable.getMinimumHeight());
        initData();
        manager = getActivity().getSupportFragmentManager();
        transaction = manager.beginTransaction();
        transaction.replace(R.id.song_sheet_relative, SongSheetClassifyFragment.newInstance(URL.SONG_SHEET_HOT_START_URL, URL.SONG_SHEET_HOT_END_URL));
        transaction.commit();
    }

    private void initData() {
        songSheetListGridZero.setOnItemClickListener(this);
        songSheetListGridOne.setOnItemClickListener(this);
        songSheetListGridTwo.setOnItemClickListener(this);
        songSheetListGridThree.setOnItemClickListener(this);
        songSheetListGridFour.setOnItemClickListener(this);
        songSheetListTotal.setOnClickListener(this);
        StringRequestUtf stringRequestUtf = new StringRequestUtf(URL.SONG_SHEET_LIST, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Gson gson = new Gson();
                SongSheetList songSheetList = gson.fromJson(response, SongSheetList.class);
                dataEntities = songSheetList.getData();
                // 第一部分
                adapter = new SongSheetListAdapter(getActivity(), dataEntities.get(0).getChildren());
                songSheetListGridZero.setAdapter(adapter);

                // 第二部分
                adapter = new SongSheetListAdapter(getActivity(), dataEntities.get(1).getChildren());
                songSheetListGridOne.setAdapter(adapter);

                // 第三部分
                adapter = new SongSheetListAdapter(getActivity(), dataEntities.get(2).getChildren());
                songSheetListGridTwo.setAdapter(adapter);

                // 第四部分
                adapter = new SongSheetListAdapter(getActivity(), dataEntities.get(3).getChildren());
                songSheetListGridThree.setAdapter(adapter);

                // 第五部分
                adapter = new SongSheetListAdapter(getActivity(), dataEntities.get(4).getChildren());
                songSheetListGridFour.setAdapter(adapter);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        volleySingleton.addQueue(stringRequestUtf);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        String text = (String) songSheetTotalTv.getText();
        try {
            list = URLEncoder.encode(text, "utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        switch (checkedId) {
            case R.id.song_sheet_radio_button_hot:
                try {
                    classify = URLEncoder.encode(StringResouce.HOT, "utf-8");
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
                if (text.equals(StringResouce.TOTAL)) {
                    transaction = manager.beginTransaction();
                    transaction.replace(R.id.song_sheet_relative, SongSheetClassifyFragment.newInstance(URL.SONG_SHEET_HOT_START_URL, URL.SONG_SHEET_HOT_END_URL));
                    transaction.commit();
            } else {
                    transaction = manager.beginTransaction();
                    transaction.replace(R.id.song_sheet_relative, SongSheetClassifyFragment.newInstance(URL.SONG_SHEET_LIST_START, URL.SONG_SHEET_LIST_PAGE_END + classify + URL.SONG_SHEET_LIST_MIDDLE + list + URL.SONG_SHEET_LIST_END));
                    transaction.commit();
                }
                break;
            case R.id.song_sheet_radio_button_recently:
                try {
                    classify = URLEncoder.encode(StringResouce.NEW, "utf-8");
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
                if (text.equals(StringResouce.TOTAL)) {
                    transaction = manager.beginTransaction();
                    transaction.replace(R.id.song_sheet_relative, SongSheetClassifyFragment.newInstance(URL.SONG_SHEET_RECENTLY_START_URL, URL.SONG_SHEET_RECENTLY_END_URL));
                    transaction.commit();
                } else {
                    transaction = manager.beginTransaction();
                    transaction.replace(R.id.song_sheet_relative, SongSheetClassifyFragment.newInstance(URL.SONG_SHEET_LIST_START, URL.SONG_SHEET_LIST_PAGE_END + classify + URL.SONG_SHEET_LIST_MIDDLE + list + URL.SONG_SHEET_LIST_END));
                    transaction.commit();
                }
                break;
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.song_sheet_total_tv:

                if (songSheetListLinear.getVisibility() == View.GONE) {
                    songSheetListTagZero.setText(dataEntities.get(0).getTag());
                    songSheetListTagOne.setText(dataEntities.get(1).getTag());
                    songSheetListTagTwo.setText(dataEntities.get(2).getTag());
                    songSheetListTagThree.setText(dataEntities.get(3).getTag());
                    songSheetListTagFour.setText(dataEntities.get(4).getTag());
                    songSheetListLinear.setVisibility(View.VISIBLE);
                    songSheetTotalTv.setCompoundDrawables(null, null, bottom_drawable, null);
                    songSheetTotalTv.setCompoundDrawablePadding(3);
                } else {
                    songSheetListLinear.setVisibility(View.GONE);
                    songSheetTotalTv.setCompoundDrawables(null, null, right_drawable, null);
                    songSheetTotalTv.setCompoundDrawablePadding(3);
                }
                break;
            case R.id.song_sheet_list_total:
                songSheetListLinear.setVisibility(View.GONE);
                songSheetTotalTv.setCompoundDrawables(null, null, right_drawable, null);
                songSheetTotalTv.setCompoundDrawablePadding(3);
                songSheetTotalTv.setText(StringResouce.TOTAL);
                break;
        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        SongSheetList.DataEntity.ChildrenEntity childrenEntity = (SongSheetList.DataEntity.ChildrenEntity) parent.getItemAtPosition(position);
        String tag = childrenEntity.getTag();
        try {
            list = URLEncoder.encode(tag, "utf-8");
            if (songSheetRadioButtonHot.isChecked() == true) {
                classify = URLEncoder.encode(StringResouce.HOT, "utf-8");
                transaction = manager.beginTransaction();
                transaction.replace(R.id.song_sheet_relative, SongSheetClassifyFragment.newInstance(URL.SONG_SHEET_LIST_START, URL.SONG_SHEET_LIST_PAGE_END + classify + URL.SONG_SHEET_LIST_MIDDLE + list + URL.SONG_SHEET_LIST_END));
                transaction.commit();
                songSheetListLinear.setVisibility(View.GONE);
                songSheetTotalTv.setCompoundDrawables(null, null, right_drawable, null);
                songSheetTotalTv.setCompoundDrawablePadding(3);
                songSheetTotalTv.setText(tag);
            } else {
                classify = URLEncoder.encode(StringResouce.NEW, "utf-8");
                transaction = manager.beginTransaction();
                transaction.replace(R.id.song_sheet_relative, SongSheetClassifyFragment.newInstance(URL.SONG_SHEET_LIST_START, URL.SONG_SHEET_LIST_PAGE_END + classify + URL.SONG_SHEET_LIST_MIDDLE + list + URL.SONG_SHEET_LIST_END));
                transaction.commit();
                songSheetListLinear.setVisibility(View.GONE);
                songSheetTotalTv.setCompoundDrawables(null, null, right_drawable, null);
                songSheetTotalTv.setCompoundDrawablePadding(3);
                songSheetTotalTv.setText(tag);
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }
}
